package com.xnfh.login

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.xnfh.common.constant.ArouterPath
import com.xnfh.common.manager.UserInfoManager
import com.xnfh.common.provider.HomeServiceProvider
import com.xnfh.framework.base.BaseActivity
import com.xnfh.framework.bean.BaseResp
import com.xnfh.framework.toast.TipsToast
import com.xnfh.framework.utils.ActManager
import com.xnfh.framework.utils.AppExit
import com.xnfh.login.databinding.ActivityLoginLayoutBinding
import com.xnfh.login.viewmodel.LoginModel
import com.xnfh.network.manager.TokenManager

/**
 * @Description:登录页面
 * @Author: ziheng
 * @CreateDate: 2023/11/8 09:16
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/11/8 09:16
 * @UpdateRemark: 更新说明
 */
@Route(path = ArouterPath.LOGIN.LOGIN_ACTIVITY)
class LoginActivity : BaseActivity<ActivityLoginLayoutBinding>(), View.OnClickListener {

    companion object {
        fun start(context: Context) {
            ActManager.Instance().popAllActivityNoExit()
            val intent = Intent(context, LoginActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun getLayoutID(): Int = R.layout.activity_login_layout

    private lateinit var viewModel: LoginModel

    override fun init(savedInstanceState: Bundle?) {
        mBind.loginHelpBut.setOnClickListener(this)
        mBind.loginGologinBut.setOnClickListener(this)
        observeLoginButtonBg()
    }

    /**
     * 控制登录按钮是否可以点击
     */
    private fun observeLoginButtonBg() {
        mBind.loginPasswordEdit.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (!TextUtils.isEmpty(s.toString().trim { it <= ' ' }) && s.toString()
                        .trim { it <= ' ' }.length >= 6
                ) {
                    mBind.loginGologinBut.isClickable = true
                    mBind.loginGologinBut.setBackgroundResource(R.drawable.user_login_select_shape)
                    mBind.loginGologinBut.setTextColor(resources.getColor(R.color.white))
                } else {
                    mBind.loginGologinBut.isClickable = false
                    mBind.loginGologinBut.setBackgroundResource(R.drawable.user_login_shape)
                    mBind.loginGologinBut.setTextColor(resources.getColor(R.color.color_B9C1CA))
                }
            }

            override fun afterTextChanged(s: Editable?) {

            }

        })
        mBind.loginUsernameEdit.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (!TextUtils.isEmpty(s.toString().trim { it <= ' ' }) && s.toString()
                        .trim { it <= ' ' }.length >= 6
                ) {
                    mBind.loginGologinBut.isClickable = true
                    mBind.loginGologinBut.setBackgroundResource(R.drawable.user_login_select_shape)
                    mBind.loginGologinBut.setTextColor(resources.getColor(R.color.white))
                } else {
                    mBind.loginGologinBut.isClickable = false
                    mBind.loginGologinBut.setBackgroundResource(R.drawable.user_login_shape)
                    mBind.loginGologinBut.setTextColor(resources.getColor(R.color.color_B9C1CA))
                }
            }

            override fun afterTextChanged(s: Editable?) {
            }

        })
    }

    override fun observe() {
        viewModel = LoginModel()
        viewModel.loginResData.observe(this) {
            if (it.responseState == BaseResp.ResponseState.REQUEST_SUCCESS) {
                //保存token
                it.data?.let { it1 -> TokenManager.saveUserToken(it1) }
                viewModel.requestUserInfor()
            }
        }
        viewModel.loginInforResData.observe(this) {
            UserInfoManager.instance.updateUserInfor(it.data)
            ARouter.getInstance().build(ArouterPath.HOME.MAIN_ACTIVITY).navigation()
            finish()
        }
    }

    override fun onBackPressed() {
        AppExit.onBackPressed(
            this,
            { TipsToast.showTips(getString(R.string.app_exit_one_more_press)) }
        )
    }

    override fun onClick(v: View?) {
        if (v == mBind.loginHelpBut) {
            LoginHelpActivity.start(this)
        } else if (v == mBind.loginGologinBut) {
            val username = mBind.loginUsernameEdit.text.toString()
            if (TextUtils.isEmpty(username)) {
                TipsToast.showTips(R.string.login_username_toast_tips)
                return
            }
            val password = mBind.loginPasswordEdit.text.toString()
            if (TextUtils.isEmpty(password)) {
                TipsToast.showTips(R.string.login_password_toast_tips)
                return
            }
            viewModel.requestLogin(username, password)
        }
    }
}